<?php
class IndexController extends Zend_Controller_Action
{ 
public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }
    public function indexAction()
    {
    	$this->_helper->layout()->disableLayout();///sopharat disablelayout to display login    	
//   		$tr = Application_Form_FrmLanguages::getCurrentlanguage();
   		$db = new Application_Model_DbTable_DbGlobal();
 		if($this->getRequest()->isPost()){
 			$post = $this->getRequest()->getPost(); 			
 			$db_user = new Application_Model_DbTable_DbUsers();
			$email = $post['txt_email'];
 			$password = $post['txt_password'];			
 			if($db_user->checkEmail($email)){
 				if($db_user->userAuthenticate($email,$password)){										
 					$user_id = $db_user->getUserID($email);
					$user_info = $db_user->getUserInfo($user_id);
					$arr_acl = $db_user->getArrAcl($user_info['user_type_id']);
					//in case user have no right to access any module of the system
					if(!$arr_acl) {$this->view->msg = (' Login User not Module ! ');}
					else{
						$session_user = new Zend_Session_Namespace('auth');
						$session_user->unlock();
						$session_user->user_id = $user_id;
						$session_user-> branch = $user_info['branch_id'];
						$session_user-> id_code = $user_info['id_code'];
						$session_user-> fullname = $user_info['fullname'];
						$session_user->user_name=$user_info['username'];
						$session_user->level = $user_info['user_type_id'];
						$session_parent->parent = $user_info['parent_id'];
						$session_user->user_type = $user_info['user_type'];
						$session_user->email = $email;
						for($i=0; $i<count($arr_acl);$i++){
							$arr_module[$i] = $arr_acl[$i]['module'];
						}
						$arr_module = array_unique($arr_module);
						$session_user->arr_acl = $arr_acl;
						$session_user->arr_module = $arr_module;												
						$session_user->lock();
						//echo $session_user->user_name;
						//echo $session_user->user_id=$user_id;exit();
						$_url=($arr_acl[0]!=='')? '/'.$arr_acl[0]['module']:'/default/index/home' ;//before
						//print_r($user_info['user_type_id']);exit();
						for ($i=0; $i<$user_info['user_type_id'];$i++){
							if($i==1){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home';
							}
							elseif($i==2){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($$i==3){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($i==4){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($i==5){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($i==6){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($i==7){
								$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
							}
							elseif($i== 8){
								$_url=($arr_acl[0]!=='')? '/foundation/index/index':'/foundation/index/index';
							}
						}
						
						$this->_redirect($_url);
					}
				}
				elseif (!$db_user->checkStatusByEmail($email)){
					$this->view->msg = (' Login  Fall Comfirm ! ');
				}
				else{
					$this->view->msg = (' Login  not Success ! ');
 				}
 			} else{
				$this->view->msg  = (' Email Incorect ! ');
			}
		}
	}		 
 	public function logoutAction()
    {
    	if($this->getRequest()->getParam('value')==1){
    		$aut=Zend_Auth::getInstance();
    		$aut->clearIdentity();
    		$session_user=new Zend_Session_Namespace('auth');
    		$session_user->unsetAll();
    		$session_stock=new Zend_Session_Namespace('stock');
    		$session_stock->unsetAll();
    		$this->_redirect('/');
    		exit();
    	}
    }
    public function webElementsAction(){	
    }
    public function homeAction(){
    }
    public function dashboadAction(){
    	//$this->_helper->layout()->disableLayout();
    }
   public function headToolbarAction(){
   	echo "this is testing";
   	
   } 
}